<template>
  <div class="chat-container">
    <div class="chat-box">
      <header class="chat-header">
        <h2>AI 聊天助手</h2>
      </header>
      <div class="chat-window">
        <div
            v-for="(message, index) in chatMessages"
            :key="index"
            class="message-wrapper"
        >
          <div
              :class="[
              'message-bubble',
              message.role === 'user' ? 'user' : 'assistant',
            ]"
          >
            <p>{{ message.content }}</p>
          </div>
        </div>
      </div>
      <div class="input-area">
        <textarea
            v-model="prompt"
            placeholder="请输入内容..."
            @keydown.enter.prevent="generateText"
        ></textarea>
        <button @click="generateText">发送</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import axios from "axios";

const my_key = "sk-T6c5Bwnz67laZdm68f4411Ed73F942438fF9Bf543b34C933"; // API 密钥
const prompt = ref("");
const chatMessages = ref([]);

const generateText = async () => {
  if (!prompt.value.trim()) return;

  // 用户消息
  chatMessages.value.push({
    role: "user",
    content: prompt.value,
  });

  // 模拟API请求
  const url = "https://api.xty.app/v1/chat/completions";
  const headers = {
    "Content-Type": "application/json",
    Authorization: `Bearer ${my_key}`,
  };
  const data = {
    model: "gpt-3.5-turbo",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      ...chatMessages.value.map((msg) => ({
        role: msg.role,
        content: msg.content,
      })),
    ],
  };

  try {
    const response = await axios.post(url, data, { headers });
    const result = response.data;
    if (result.choices && result.choices.length > 0) {
      chatMessages.value.push({
        role: "assistant",
        content: result.choices[0].message.content,
      });
    } else {
      chatMessages.value.push({
        role: "assistant",
        content: "抱歉，我无法生成内容。",
      });
    }
  } catch (error) {
    console.error("Error:", error);
    chatMessages.value.push({
      role: "assistant",
      content: "请求出错，请稍后重试。",
    });
  } finally {
    prompt.value = ""; // 清空输入框
    scrollToBottom(); // 滚动到底部
  }
};

const scrollToBottom = () => {
  setTimeout(() => {
    const chatWindow = document.querySelector(".chat-window");
    chatWindow.scrollTop = chatWindow.scrollHeight;
  }, 100);
};
</script>

<style scoped lang="scss">
/* 整体布局 */
.chat-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f3f4f6;
}

.chat-box {
  width: 1200px;
  height: 660px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 标题栏 */
.chat-header {
  background: linear-gradient(90deg, #4caf50, #81c784);
  padding: 10px;
  text-align: center;
  font-size: 18px;
  color: white;
  font-weight: bold;
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
}

/* 聊天窗口 */
.chat-window {
  flex: 1;
  padding: 15px;
  overflow-y: auto;
  background: #f7f8fa;
}

/* 消息 */
.message-wrapper {
  display: flex;
  margin-bottom: 10px;
  animation: slide-in 0.3s ease-out;
}

.message-bubble {
  max-width: 75%;
  padding: 10px 15px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  position: relative;
}

.user {
  align-self: flex-end;
  background: #4caf50;
  color: white;
  border-top-right-radius: 0;
}

.assistant {
  align-self: flex-start;
  background: #e4e6eb;
  color: #333;
  border-top-left-radius: 0;
}

/* 输入区域 */
.input-area {
  display: flex;
  padding: 10px;
  background: #fff;
  border-top: 1px solid #ddd;
}

textarea {
  flex: 1;
  resize: none;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}

textarea:focus {
  border-color: #4caf50;
}

button {
  margin-left: 10px;
  padding: 10px 15px;
  background: linear-gradient(90deg, #4caf50, #66bb6a);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

button:hover {
  background: #45a045;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

button:active {
  transform: scale(0.95);
}

/* 动画 */
@keyframes slide-in {
  from {
    transform: translateY(10px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>
